<?php

/**
 * FootballFactory, Copyright Robin Loop & Pascal Wirz, 2014
 * ---
 * Repraesentiert einen Torschuetzen
 * Methoden, um Torschuetzen aus der DB abzurufen, editieren und loeschen
 *
 * @author Lopo
 */
class Scorer {
    private $id = null;
    private $player_id = null;
    private $game_id = null;

	public function __construct($data = array()) {
        if (isset($data['scorers_id'])) $this->id = (int) $data['scorers_id'];
        if (isset($data['player_id'])) $this->player_id = $data['player_id'];
        if (isset($data['game_id'])) $this->game_id = $data['game_id'];
    }
	
	public static function getScorerById($id) {
        $db = new Database();

        $sql = "SELECT * FROM ff_scorers WHERE scorers_id = :id";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $id, PDO::PARAM_INT);
        $stmt->execute();
        $row = $stmt->fetch();
        
        $db = null;

        if ($row) return new Scorer($row);
    }
	
	public static function getScorersByTeamAndSeason($team_id, $season_id) {
        $db = new Database();

        $sql = "SELECT * FROM ff_scorers sc, ff_players p, ff_games g, ff_seasons s WHERE" +
		" sc.player_id=p.player_id AND sc.game_id = g.game_id AND g.game_season = :season_id AND p.player_team = :team_id ";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":team_id", $team_id, PDO::PARAM_INT);
        $stmt->bindValue(":season_id", $season_id, PDO::PARAM_INT);
        $stmt->execute();
        
        $list = array();
        while ($row = $stmt->fetch()) {
            $scorer = new Scorer($row);
            $list[] = $scorer;
        }

        $sql = "SELECT FOUND_ROWS() AS totalRows";
        $totalRows = $db->query($sql)->fetch();
        
        $db = null;
        
        return (array("scorerList" => $list, "totalRows" => $totalRows[0]));
    }
    
    public static function getScorersByGameId($gameId) {
        $db = new Database();
        
        $sql = "SELECT * FROM ff_scorers WHERE game_id = :gameId";
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":gameId", $gameId, PDO::PARAM_INT);
        $stmt->execute();
        
        $list = array();
        while ($row = $stmt->fetch()) {
            $scorer = new Scorer($row);
            $list[] = $scorer;
        }

        $sql = "SELECT FOUND_ROWS() AS totalRows";
        $totalRows = $db->query($sql)->fetch();
        
        $db = null;
        
        return (array("scorerList" => $list, "totalRows" => $totalRows[0]));
    }
	
	public function insert() {
        if (!is_null($this->id)) {
            trigger_error("Scorer::insert(): Es wird versucht einen Torschuetzen zu speichern, der eine ID hat, die bereits existiert (ID: $this->id).", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "INSERT INTO ff_scorers (player_id, game_id) VALUES (:player_id, :game_id)";
		
		$stmt = $db->prepare($sql);
        $stmt->bindValue(":player_id", $this->player_id, PDO::PARAM_INT);
        $stmt->bindValue(":game_id", $this->game_id, PDO::PARAM_INT);
        $stmt->execute();
        
        $this->id = $db->lastInsertId();
        
        $db = null;
    }
	
	public function update() {
        if (is_null($this->id)) {
            trigger_error("Scorer::update(): Es wird versucht einen Torschuetzen zu speichern, der keine ID hat - was nicht geht beim Update.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "UPDATE ff_scorers SET player_id=:player_id, game_id=:game_id WHERE scorers_id=:id";
		
        $stmt = $db->prepare($sql);
		$stmt->bindValue(":player_id", $this->player_id, PDO::PARAM_INT);
        $stmt->bindValue(":game_id", $this->game_id, PDO::PARAM_INT);
		$stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }
	
	public function delete() {
        if (is_null($this->id)) {
            trigger_error("Scorer::delete(): Es wird versucht einen Torschuetzen zu loeschen, der keine ID hat - was nicht geht beim Loeschen.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "DELETE FROM ff_scorers WHERE scorers_id = :id LIMIT 1";
        
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }
    public function getId() {
        return $this->id;
    }

    public function getPlayer_id() {
        return $this->player_id;
    }

    public function getGame_id() {
        return $this->game_id;
    }


    
    
}
?>
